﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AplicacionMVC.Controllers
{
    public class VeterinarioController : Controller
    {
        private VeterinarioWS.VeterinarioClient proxy = new VeterinarioWS.VeterinarioClient();
        
        /// <summary>
        /// GET INDEX
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            List<VeterinarioWS.Veterinario> v = proxy.ListarVeterinario();
            return View(v);
            
        }

        /// <summary>
        /// POST INDEX
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Details(int id)
        {
            VeterinarioWS.Veterinario v = proxy.ObtenerVeterinario(id);
            return View(v);
        }

        /// <summary>
        /// GTE CREATE
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            return View();
        } 

       /// <summary>
       /// POST CREATE
       /// </summary>
       /// <param name="collection"></param>
       /// <returns></returns>
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                string nombre = collection["txtVeterinarioNombre"];
                string apaterno = collection["txtVeterinarioApellidoP"];
                string amaterno = collection["txtVeterinarioApellidoM"];
                string numCVP = collection["numCVP"];

                VeterinarioWS.Veterinario v = proxy.CrearVeterinario(nombre, apaterno, amaterno, numCVP);

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, "Error: " + ex.Message.ToString());
                return View();
            }
        }
        
        /// <summary>
        /// GET EDIT
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            try
            {
                VeterinarioWS.Veterinario v = proxy.ObtenerVeterinario(id);
                return View(v);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, "Error: " + ex.Message.ToString());
                return View();
            }
            
        }

        /// <summary>
        /// POST EDIT
        /// </summary>
        /// <param name="id"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                int codigo = int.Parse( collection["IdVeterinario"]);
                string nombre = collection["txtVeterinarioNombre"];
                string apaterno = collection["txtVeterinarioApellidoP"];
                string amaterno = collection["txtVeterinarioApellidoM"];
                string numCVP = collection["numCVP"];
                VeterinarioWS.Veterinario v = proxy.ModificarVeterinario(codigo, nombre, apaterno, amaterno, numCVP);    
 
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                //mensaje de error
                ModelState.AddModelError(string.Empty, "Error: " + ex.Message.ToString());
                return View();
            }
        }

        /// <summary>
        /// GET DELETE
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(int id)
        {
            try
            {
                VeterinarioWS.Veterinario v = proxy.ObtenerVeterinario(id);
                return View(v);
            }
            catch (Exception ex)
            {
                //mensaje de error
                ModelState.AddModelError(string.Empty, "Error: " + ex.Message.ToString());
                return View();
            }
        }

        /// <summary>
        /// POST DELETE
        /// </summary>
        /// <param name="id"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                proxy.EliminarVeterinario(id);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                //mensaje de error
                ModelState.AddModelError(string.Empty, "Error: " + ex.Message.ToString());
                return RedirectToAction("Index");
            }
        }
    }
}
